<?php

namespace App\Http\Controllers\Admin\Export;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Exports\UsersExport;
use DummyFullModelClass;
use App\lain;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Users;

use Excel;
/**
 * 分享管理控制器
 *
 * @author yh
 * @package App\Http\Controllers\Exports
 */
class ExportsController extends BaseAdminController
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index(lain $lain)
    {
        //
       
        $sort = request('sort', 'uid');
        $order = request('order', 'desc');
        $query = Users::orderBy($sort, $order);
        // 取得列表
        $list=$query->select([
            'uid',
            'username',
            'nickname',
            'avatar',
            'phone',
            'openid',
            'created_at'
        ])->get()->map(function ($value) {
            return (array)$value;
        })->toArray();
        $headings=['uid','姓名','昵称','头像','手机号','openId','时间'];
        $title=date('Y-m-d',time()).'用户表';
        $width=array('A'=>30,'B'=>15,'C'=>15,'D'=>15,'E'=>20,'F'=>40);
        return  Excel::download(new UsersExport($list, $headings), $title.'.xls');
    }
     /**
     * 添加用户
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20201104
     */
    public function add()
    {
        return view('admin.share.shares_form');
    } 
     /**
     * 保存用户
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170206
     */
    public function save()
    {
       // 保存用户信息
		DB::transaction(function () {
            $user = Shares::findOrNew(request('id'));
            if(empty(request('img'))){
                $rule = [
                    'img' => 'required|shares,img',
                ];
                $this->validate(request(), $rule, [], [
                    'img' => '分享主图'
                ]);
            }
            
			$user->title = request('title');
			$user->img = request('img');
			$user -> timestamps = true;
			$user -> save();
			// 更新附件id
			if (!request('id')) {
				// 更新头像
				if (intval(request('share_file_id'))) {
					UploadedFile::where('file_id', intval(request('share_file_id')))->update(['item_id' => $user->id]);
				}
			}
		});

		return $this->success(get_session_url('index'));
    }
    /**
     * 删除分享
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Shares::destroy($id);
            return $this->success();
        }
    }
    /**
	 * 编辑分享
	 *
	 * @author lxp 20170111
	 * @param $uid
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 */
	public function edit($id)
	{
		// 取得用户信息
		$user = Shares::findOrFail($id);

		return view('admin.share.shares_form', [
			'user' => $user
		]);
	}

    /**
     * 禁用/启用分享
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author yh 20201104
     */
    public function change($uid)
    {
        if (request()->ajax() && intval($uid)) {
            // 判断用户是否可以被删除

            // 禁用分享
            $users = Shares::where('id', $uid)->first();
            if ($users->status == 1) {
                $users->status = 2;
            } else {
                $users->status = 1;
            }
            $users->save();
        }
        return $this->success();
    }
}